#include "dock.h"
#include "database.h"

Dock::Dock(QWidget *parent) : QWidget(parent)
{
    setMinimumWidth(250);
    setMaximumWidth(250);

    QVBoxLayout* stackedLayout = new QVBoxLayout;
    QHBoxLayout* searchLayout = new QHBoxLayout;
    searchButton = new QToolButton(this);
    searchButton->setIcon(QIcon("search.png"));
    //searchButton->setAutoDefault(true);
    searchEdit = new QLineEdit(this);
    searchLayout->addWidget(searchEdit);
    searchLayout->addWidget(searchButton);

    playlists = new QListWidget(this);
    playlists->setFocusPolicy(Qt::NoFocus);
    stackedLayout->addLayout(searchLayout);
    stackedLayout->addWidget(playlists);

    stackedLayout->addWidget(VideoPlayer::get(), 0, Qt::AlignBottom);
    setLayout(stackedLayout);

    QObject::connect(searchButton, SIGNAL(clicked()), this, SLOT(searchClicked()));
    QObject::connect(searchEdit, SIGNAL(returnPressed()), searchButton, SLOT(click()));
    QObject::connect(playlists, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(playlistClicked(QListWidgetItem*)));
}


void Dock::playlistClicked(QListWidgetItem* item)
{
    emit(playlistClicked(item->data(Qt::DisplayRole).toString()));
}

void Dock::searchClicked()
{
    if(!searchEdit->text().isEmpty())
        emit(searchRequested(searchEdit->text()));
}

void Dock::addPlaylist(QString name)
{
    playlists->addItem(name);
}
